package com.hongdee.crm.workorder.service.impl;


import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.dao.IImpHibernateDao;
import com.hongdee.common.dao.IImpJdbcDao;
import com.hongdee.common.dao.IJdbcDao;
import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.saMessage.service.ITCrmCarSaMessageService;
import com.hongdee.crm.task.service.ICarBaseCountService;
import com.hongdee.crm.task.service.ITCrmCarWorkorderTaskService;
import com.hongdee.crm.workorder.dao.ITCrmCarWorkOrderDao;
import com.hongdee.crm.workorder.service.ITCrmCarWorkOrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;


@Service
public class TCrmCarWorkOrderQueryServiceImpl implements ITCrmCarWorkOrderQueryService {

    private String tabName = "T_CRM_CAR_WORKORDER";

    @Autowired
    private ITCrmCarWorkOrderDao iTCrmCarWorkOrderDao;

    @Override
    public Map impPage(String companyId,String orderNum, String start, String limit, String platenum, String vin, String balanceDateStart,String balanceDateEnd,String uid) throws Exception {
        StringBuffer sqlBuffer = new StringBuffer();
        //String oId = TokenUtil.getCompanyId(uid);
        sqlBuffer.append("select * from " + tabName + " where 1=1 ");
        List<String> paramList = new ArrayList<String>();
        if (!StringUtils.isEmpty(orderNum)) {
            sqlBuffer.append(" and order_num like ? ");
            paramList.add("%"+orderNum+"%");
        }
        if (!StringUtils.isEmpty(platenum)) {
            sqlBuffer.append(" and plate_num like ? ");
            paramList.add("%"+platenum+"%");
        }
        if (!StringUtils.isEmpty(vin)) {
            sqlBuffer.append(" and vin like ? ");
            paramList.add("%"+vin+"%");
        }
        if (!StringUtils.isEmpty(balanceDateStart)) {
            sqlBuffer.append(" and from_unixtime(balance_date/1000, '%Y-%m-%d') >= ? ");
            paramList.add(balanceDateStart);
        }
        if(!StringUtils.isEmpty(balanceDateEnd)){
            sqlBuffer.append(" and  from_unixtime(balance_date/1000, '%Y-%m-%d') <= ? ");
            paramList.add(balanceDateEnd);
        }
        if(!StringUtils.isEmpty(companyId)){
            sqlBuffer.append(" and company_id = ?");
            paramList.add(companyId);
        }
        else {
            String oId = TokenUtil.getCompanyId(uid);
            sqlBuffer.append(" and company_id = ?");
            paramList.add(oId);
        }

        sqlBuffer.append(" order by balance_date desc ");
        return this.iTCrmCarWorkOrderDao.page(sqlBuffer.toString(), paramList.toArray(),start, limit );
    }


    /**
     * @Name:工单表
     * @Date: 2017/8/17 13:45
     * @Author: Liu XiaoLong
     */
    @Override
    public Map workOrderTable(String uid, String maintainer, String maintainDate, String plateNumber,String start,String limit,String dateStart,String dateStop) throws Exception {
        StringBuffer sql = new StringBuffer();
        List<String> param = new ArrayList<String>();
        String companyId = TokenUtil.getCompanyId(uid);
        sql.append("SELECT balance_amount balanceAmount,balance_date balanceDate,vin,type,order_num orderNum,");
        sql.append("maintain_type maintainType,maintain_amount maintainAmount,plate_num plateNum FROM "+tabName+" WHERE 1=1 ");
        if(!plateNumber.isEmpty()){
            sql.append("AND plate_num LIKE ? ");
            param.add("%"+plateNumber+"%");
        }
        if(!maintainer.isEmpty()){
            sql.append("AND sender LIKE ? ");
            param.add("%"+maintainer+"%");
        }
        if(!companyId.isEmpty()){
            sql.append("AND company_id = ? ");
            param.add(companyId);
        }
        if(!dateStart.isEmpty()){   //起始时间
            Long dateStartL = DateUtils.dayToLongTime(dateStart);
            sql.append("and create_time>=? ");
            param.add(dateStartL.toString());
        }
        if(!dateStop.isEmpty()){    //结束时间
            Long dateStopL = DateUtils.dayToLongTime(dateStop);
            sql.append("and create_time<=? ");
            param.add(dateStopL.toString());
        }
        if(!maintainDate.isEmpty()){
            Long dateLong = DateUtils.dayToLongTime(maintainDate);
            sql.append("AND vin IN (SELECT vin FROM t_crm_car WHERE last_maintain_date = ?)");
            param.add(dateLong.toString());
        }

        return this.iTCrmCarWorkOrderDao.page(sql.toString(), param.toArray(),start, limit );
    }
}